package com.chinasoft.goodsManager.service.impl;

import com.chinasoft.goodsManager.dao.LoginDao;
import com.chinasoft.goodsManager.dao.impl.LoginDaoImpl;
import com.chinasoft.goodsManager.domain.Goods;
import com.chinasoft.goodsManager.domain.Manager;
import com.chinasoft.goodsManager.service.LoginService;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoginServiceImpl implements LoginService {
    LoginDao loginDao = new LoginDaoImpl();

    @Override
    public boolean findMUserName(String musername) {
        Set<String> musernames = loginDao.findMUserNameByName();
        for (String name : musernames) {
            if (name.equals(musername)){
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean findMEmail(String memail) {
        Set<String> memails = loginDao.findMEmailByEmail();
        if (memails.equals(memail)) {
            return false;
        }
        return true;
    }

    @Override
    public void addNewManager(String musername, String memail, String mpassword) {
        loginDao.addNewManager(musername,memail,mpassword);
    }

    //登录验证
    @Override
    public Manager fandUsernameAndPassword(String musernameOrMemail, String mpassword) {
        //邮箱正则
        String format = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$" ;
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(musernameOrMemail);
        //是否为邮箱登录
        if (matcher.matches()){
            return fandManagerByEmail(musernameOrMemail,mpassword);
        }else {
            return fandManagerByUsername(musernameOrMemail, mpassword);
        }
    }

    @Override
    public Manager updateManagerInformation(Manager manager, String mname, String mage, String mcity, String maddress, String mphone) {
        manager.setMname(mname);
        manager.setMage(mage);
        manager.setMcity(mcity);
        manager.setMaddress(maddress);
        manager.setMphone(mphone);
        return loginDao.updateManagerInformation(manager);
    }

    @Override
    public Manager updateManagerImg(String filename, Integer id) {
        return loginDao.updateManagerImg(filename,id);
    }

    @Override
    public Goods findGoodsById(Integer id) {
        return loginDao.findGoodsById(id);
    }

    @Override
    public void addGoods(Goods goods) {
        if (goods.getGimg()==null||goods.getGimg().equals("")){
            goods.setGimg("4.jpg");
        }
        loginDao.addGoods(goods);
    }

    @Override
    public void updateGoods(Goods goods) {
        loginDao.updateGoods(goods);
    }

    //通过用户名查找对象
    private Manager fandManagerByUsername(String musernameOrMemail, String mpassword) {
        return loginDao.findMUserNameAndPassword(musernameOrMemail,mpassword);
    }

    //通过邮箱查找对象
    private Manager fandManagerByEmail(String musernameOrMemail, String mpassword) {
        return loginDao.findEmailAndPassword(musernameOrMemail, mpassword);
    }

    //初始化redis数据库
    public void transferDatabase(){
        List<Map<String, Object>> mUserName = loginDao.findMUserName();
        List<Map<String, Object>> mEmail = loginDao.findMEmail();
        for (Map<String, Object> map : mUserName) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                loginDao.setRedisName(String.valueOf(map.get(key)));
            }
        }
        for (Map<String, Object> map : mEmail) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                loginDao.setRedisEmail(String.valueOf(map.get(key)));
            }
        }
    }
}
